package com.zhaoxi.project.cms.blog.controller;

import com.zhaoxi.common.utils.StringUtils;
import com.zhaoxi.common.utils.poi.ExcelUtil;
import com.zhaoxi.framework.aspectj.lang.annotation.Log;
import com.zhaoxi.framework.aspectj.lang.enums.BusinessType;
import com.zhaoxi.framework.web.controller.BaseController;
import com.zhaoxi.framework.web.domain.AjaxResult;
import com.zhaoxi.framework.web.page.TableDataInfo;
import com.zhaoxi.project.cms.blog.domain.CmsBlog;
import com.zhaoxi.project.cms.blog.service.impl.CmsBlogServiceImpl;
import com.zhaoxi.project.cms.tag.domain.CmsTag;
import com.zhaoxi.project.cms.tag.service.impl.CmsTagServiceImpl;
import com.zhaoxi.project.cms.type.domain.CmsType;
import com.zhaoxi.project.cms.type.service.impl.CmsTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客信息Controller
 *
 * @author zhaoxi
 * @date 2023-07-25
 */
@RestController
@RequestMapping("/cms/blog")
public class CmsBlogController extends BaseController {
    @Autowired
    private CmsBlogServiceImpl cmsBlogService;
    @Autowired
    private CmsTypeServiceImpl cmsTypeService;
    @Autowired
    private CmsTagServiceImpl cmsTagService;

    /**
     * 查询博客信息列表
     */
    @PreAuthorize("@ss.hasPermi('cms:blog:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsBlog cmsBlog) {
        startPage();
        List<CmsBlog> list = cmsBlogService.selectCmsBlogList(cmsBlog);
        return getDataTable(list);
    }

    /**
     * 导出博客信息列表
     */
    @PreAuthorize("@ss.hasPermi('cms:blog:export')")
    @Log(title = "博客信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsBlog cmsBlog) {
        List<CmsBlog> list = cmsBlogService.selectCmsBlogList(cmsBlog);
        ExcelUtil<CmsBlog> util = new ExcelUtil<CmsBlog>(CmsBlog.class);
        util.exportExcel(response, list, "博客信息数据");
    }

    /**
     * 获取博客信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:blog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cmsBlogService.selectCmsBlogById(id));
    }

    /**
     * 新增博客信息
     */
    @PreAuthorize("@ss.hasPermi('cms:blog:add')")
    @Log(title = "博客信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsBlog cmsBlog) {
        return toAjax(cmsBlogService.insertCmsBlog(cmsBlog));
    }

    /**
     * 修改博客信息
     */
    @PreAuthorize("@ss.hasPermi('cms:blog:edit')")
    @Log(title = "博客信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsBlog cmsBlog) {
        System.out.println("cmsBlog==>" + cmsBlog);
        return toAjax(cmsBlogService.updateCmsBlog(cmsBlog));
    }

    /**
     * 删除博客信息
     */
    @PreAuthorize("@ss.hasPermi('cms:blog:remove')")
    @Log(title = "博客信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsBlogService.deleteCmsBlogByIds(ids));
    }


    /*2023/7/27*/

    /**
     * 首页查询文章列表
     */
    @GetMapping("/cms/cmsList")
    public TableDataInfo cmsList(CmsBlog cmsBlog) {
        startPage();
        //状态为发布
        cmsBlog.setStatus("1");
        List<CmsBlog> list = cmsBlogService.selectCmsBlogList(cmsBlog);
        return getDataTable(list);
    }


    /**
     * 首页按分类查询文章列表
     */
    @GetMapping("/cms/cmsListByType/{id}")
    public TableDataInfo cmsListByTypeId(@PathVariable(value = "id", required = false) Long id) {
        startPage();
        List<CmsBlog> list = cmsBlogService.selectCmsBlogListByTypeId(id);
        return getDataTable(list);
    }

    /**
     * 首页按标签查询文章列表
     */
    @GetMapping("/cms/cmsListByTag/{id}")
    public TableDataInfo cmsListByTagId(@PathVariable(value = "id", required = false) Long id) {
        startPage();
        List<CmsBlog> list = cmsBlogService.selectCmsBlogListByTagId(id);
        return getDataTable(list);
    }

    /**
     * 首页查询推荐文章列表
     */
    @GetMapping("/cms/cmsListRecommend")
    public TableDataInfo cmsListRecommend(CmsBlog cmsBlog) {
        startPage();
        //状态为发布
        cmsBlog.setStatus("1");
        List<CmsBlog> list = cmsBlogService.selectCmsBlogListRecommend(cmsBlog);
        return getDataTable(list);
    }

    /*
     * 首页获取文章详细信息
     */
    /*
     * 首页获取文章详细信息
     */
    /**
     * 首页获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:blog:query')")
    @GetMapping(value = { "/cms/detail/", "/cms/detail/{id}" })
    public AjaxResult getInfoDetail(@PathVariable(value = "id", required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        CmsType cmsType = new CmsType();
        CmsTag cmsTag = new CmsTag();
        ajax.put("types", cmsTypeService.selectCmsTypeList(cmsType));
        ajax.put("tags", cmsTagService.selectCmsTagList(cmsTag));
        if (StringUtils.isNotNull(id))
        {
            ajax.put(AjaxResult.DATA_TAG, cmsBlogService.selectCmsBlogById(id));
        }
        return ajax;
    }

    /*@PreAuthorize("@ss.hasPermi('cms:blog:query')")
    @GetMapping("/cms/detail")
    public AjaxResult getInfoDetail(@RequestParam(value = "id", required = false) Long id) {
        AjaxResult ajax = AjaxResult.success();
        CmsType cmsType = new CmsType();
        CmsTag cmsTag = new CmsTag();
        ajax.put("types", cmsTypeService.selectCmsTypeList(cmsType));
        ajax.put("tags", cmsTagService.selectCmsTagList(cmsTag));
        if (id != null) {
            ajax.put(AjaxResult.DATA_TAG, cmsBlogService.selectCmsBlogById(id));
        }
        return ajax;
    }*/

   /* @PreAuthorize("@ss.hasPermi('cms:blog:query')")
    @GetMapping ("/cms/detail/{id}")
    public AjaxResult getInfoDetail(@PathVariable(value = "id") Long id) {
        AjaxResult ajax = AjaxResult.success();
        CmsType cmsType = new CmsType();
        CmsTag cmsTag = new CmsTag();
        ajax.put("types", cmsTypeService.selectCmsTypeList(cmsType));
        ajax.put("tags", cmsTagService.selectCmsTagList(cmsTag));
        if (StringUtils.isNotNull(id)) {
            ajax.put(AjaxResult.DATA_TAG, cmsBlogService.selectCmsBlogById(id));
        }
        return ajax;
    }*/

}
